<%@ page import="list.model.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String opt = request.getParameter("opt");

    String id = request.getParameter("id");
    String password = request.getParameter("password");
    String userTemp = request.getParameter("user_id");
    String tagIdTemp = request.getParameter("tag");
    if (opt != null) {
        switch (opt) {
            case "to":
                int tag = Integer.parseInt(tagIdTemp);
                response.sendRedirect("main.jsp?tag=" + tag);
                break;
            case "register":
                String email = request.getParameter("email");
                if (User.register(id, email, password)) {
                    String auth = "12345678";//生成随机字符串
                    response.sendRedirect("../main.jsp?auth=" + auth);//进入主页面
                } else {
                    response.sendRedirect("../index.jsp?opt=register&val=failed");//返回注册页面
                }
                break;
            case "login":
                if (User.login(id, password)) {
                    String auth = "12345678";//生成随机字符串
                    response.sendRedirect("../main.jsp?auth=" + auth);//进入主页面
                } else {
                    response.sendRedirect("../index.jsp?opt=login&val=failed");//返回登录页面
                }
                break;
            default:
                String auth = request.getParameter("auth");//凭证
                //由凭证后获取下面的对象
                User user = new User(1);
                String key = request.getParameter("key");//目标
                String pro = request.getParameter("pro");//属性
                String val = request.getParameter("val");//值
                int idd = Integer.parseInt(id == null ? "0" : id);//目标id
                int tag;
                switch (key) {
                    case "contact":
                        switch (opt) {
                            case "update":
                                out.print(user.updateContact(idd, pro, val));
                                break;
                            case "insert":
                                tag = tagIdTemp == null ? 0 : Integer.getInteger(tagIdTemp);
                                out.print(user.insertContact(tag));
                                break;
                            case "delete":
                                tag = tagIdTemp == null ? 0 : Integer.getInteger(tagIdTemp);
                                out.print(user.deleteContact(tag, idd));
                                break;
                        }
                        break;
                    case "tag":
                        switch (opt) {
                            case "update":
                                tag = tagIdTemp == null ? 0 : Integer.getInteger(tagIdTemp);
                                out.print(user.updateTag(tag, val));
                                break;
                            case "insert":
                                out.print(user.insertTag());
                                break;
                            case "delete":
                                tag = tagIdTemp == null ? 0 : Integer.getInteger(tagIdTemp);
                                out.print(user.deleteTag(tag));
                                break;
                        }
                        break;

                    case "user":
                        if (opt.equals("update")) {
                            switch (pro) {
                                case "phone":
                                    out.print(user.updatePhone(val));
                                    break;
                                case "birthday":
                                    out.print(user.updateBirthday(val));
                                    break;
                                case "email":
                                    out.print(user.updateEmail(val));
                                    break;
                                case "head":
                                    out.print(user.updateHead(val));
                                    break;
                                case "name":
                                    out.print(user.updateName(val));
                                    break;
                            }
                            break;
                        }
                }
                response.sendRedirect("../main.jsp");//进入主页面
        }
    }

%>